<?php

namespace App\Constants;

class AppConstants
{
    /**
     * 订单状态
     */
    const ORDER_STATUS_PENDING = 'pending';
    const ORDER_STATUS_PROCESSING = 'processing';
    const ORDER_STATUS_SHIPPED = 'shipped';
    const ORDER_STATUS_DELIVERED = 'delivered';
    const ORDER_STATUS_CANCELLED = 'cancelled';
    const ORDER_STATUS_REFUNDED = 'refunded';

    /**
     * 订单状态文本
     */
    const ORDER_STATUS_TEXTS = [
        self::ORDER_STATUS_PENDING => '待付款',
        self::ORDER_STATUS_PROCESSING => '处理中',
        self::ORDER_STATUS_SHIPPED => '已发货',
        self::ORDER_STATUS_DELIVERED => '已送达',
        self::ORDER_STATUS_CANCELLED => '已取消',
        self::ORDER_STATUS_REFUNDED => '已退款',
    ];

    /**
     * 支付状态
     */
    const PAYMENT_STATUS_PENDING = 'pending';
    const PAYMENT_STATUS_PAID = 'paid';
    const PAYMENT_STATUS_FAILED = 'failed';
    const PAYMENT_STATUS_REFUNDED = 'refunded';
    const PAYMENT_STATUS_COD_CONFIRMED = 'cod_confirmed';

    /**
     * 支付状态文本
     */
    const PAYMENT_STATUS_TEXTS = [
        self::PAYMENT_STATUS_PENDING => '待支付',
        self::PAYMENT_STATUS_PAID => '已支付',
        self::PAYMENT_STATUS_FAILED => '支付失败',
        self::PAYMENT_STATUS_REFUNDED => '已退款',
        self::PAYMENT_STATUS_COD_CONFIRMED => '货到付款已确认',
    ];

    /**
     * 支付方式
     */
    const PAYMENT_METHOD_ALIPAY = 'alipay';
    const PAYMENT_METHOD_WECHAT = 'wechat';
    const PAYMENT_METHOD_COD = 'cod';

    /**
     * 支付方式文本
     */
    const PAYMENT_METHOD_TEXTS = [
        self::PAYMENT_METHOD_ALIPAY => '支付宝',
        self::PAYMENT_METHOD_WECHAT => '微信支付',
        self::PAYMENT_METHOD_COD => '货到付款',
    ];

    /**
     * 售后类型
     */
    const AFTER_SALES_TYPE_RETURN = 'return';
    const AFTER_SALES_TYPE_REFUND = 'refund';
    const AFTER_SALES_TYPE_EXCHANGE = 'exchange';

    /**
     * 售后类型文本
     */
    const AFTER_SALES_TYPE_TEXTS = [
        self::AFTER_SALES_TYPE_RETURN => '退货',
        self::AFTER_SALES_TYPE_REFUND => '退款',
        self::AFTER_SALES_TYPE_EXCHANGE => '换货',
    ];

    /**
     * 售后状态
     */
    const AFTER_SALES_STATUS_PENDING = 'pending';
    const AFTER_SALES_STATUS_APPROVED = 'approved';
    const AFTER_SALES_STATUS_REJECTED = 'rejected';
    const AFTER_SALES_STATUS_PROCESSING = 'processing';
    const AFTER_SALES_STATUS_COMPLETED = 'completed';
    const AFTER_SALES_STATUS_CANCELLED = 'cancelled';

    /**
     * 售后状态文本
     */
    const AFTER_SALES_STATUS_TEXTS = [
        self::AFTER_SALES_STATUS_PENDING => '待审核',
        self::AFTER_SALES_STATUS_APPROVED => '已通过',
        self::AFTER_SALES_STATUS_REJECTED => '已拒绝',
        self::AFTER_SALES_STATUS_PROCESSING => '处理中',
        self::AFTER_SALES_STATUS_COMPLETED => '已完成',
        self::AFTER_SALES_STATUS_CANCELLED => '已取消',
    ];

    /**
     * 物流状态
     */
    const LOGISTICS_STATUS_PENDING = 'pending';
    const LOGISTICS_STATUS_SHIPPED = 'shipped';
    const LOGISTICS_STATUS_IN_TRANSIT = 'in_transit';
    const LOGISTICS_STATUS_DELIVERED = 'delivered';
    const LOGISTICS_STATUS_FAILED = 'failed';

    /**
     * 物流状态文本
     */
    const LOGISTICS_STATUS_TEXTS = [
        self::LOGISTICS_STATUS_PENDING => '待发货',
        self::LOGISTICS_STATUS_SHIPPED => '已发货',
        self::LOGISTICS_STATUS_IN_TRANSIT => '运输中',
        self::LOGISTICS_STATUS_DELIVERED => '已送达',
        self::LOGISTICS_STATUS_FAILED => '配送失败',
    ];

    /**
     * 用户状态
     */
    const USER_STATUS_ACTIVE = 'active';
    const USER_STATUS_INACTIVE = 'inactive';
    const USER_STATUS_BANNED = 'banned';

    /**
     * 用户状态文本
     */
    const USER_STATUS_TEXTS = [
        self::USER_STATUS_ACTIVE => '正常',
        self::USER_STATUS_INACTIVE => '未激活',
        self::USER_STATUS_BANNED => '已封禁',
    ];

    /**
     * 商品状态
     */
    const PRODUCT_STATUS_ACTIVE = 'active';
    const PRODUCT_STATUS_INACTIVE = 'inactive';
    const PRODUCT_STATUS_DRAFT = 'draft';

    /**
     * 商品状态文本
     */
    const PRODUCT_STATUS_TEXTS = [
        self::PRODUCT_STATUS_ACTIVE => '上架',
        self::PRODUCT_STATUS_INACTIVE => '下架',
        self::PRODUCT_STATUS_DRAFT => '草稿',
    ];

    /**
     * 通知类型
     */
    const NOTIFICATION_TYPE_ORDER = 'order';
    const NOTIFICATION_TYPE_AFTER_SALES = 'after_sales';
    const NOTIFICATION_TYPE_SYSTEM = 'system';
    const NOTIFICATION_TYPE_ADMIN = 'admin';

    /**
     * 通知类型文本
     */
    const NOTIFICATION_TYPE_TEXTS = [
        self::NOTIFICATION_TYPE_ORDER => '订单通知',
        self::NOTIFICATION_TYPE_AFTER_SALES => '售后通知',
        self::NOTIFICATION_TYPE_SYSTEM => '系统通知',
        self::NOTIFICATION_TYPE_ADMIN => '管理员消息',
    ];

    /**
     * 文件类型
     */
    const FILE_TYPE_IMAGE = 'image';
    const FILE_TYPE_VIDEO = 'video';
    const FILE_TYPE_DOCUMENT = 'document';
    const FILE_TYPE_AUDIO = 'audio';
    const FILE_TYPE_OTHER = 'other';

    /**
     * 文件类型文本
     */
    const FILE_TYPE_TEXTS = [
        self::FILE_TYPE_IMAGE => '图片',
        self::FILE_TYPE_VIDEO => '视频',
        self::FILE_TYPE_DOCUMENT => '文档',
        self::FILE_TYPE_AUDIO => '音频',
        self::FILE_TYPE_OTHER => '其他',
    ];

    /**
     * 权限类型
     */
    const PERMISSION_TYPE_MENU = 'menu';
    const PERMISSION_TYPE_BUTTON = 'button';
    const PERMISSION_TYPE_API = 'api';

    /**
     * 权限类型文本
     */
    const PERMISSION_TYPE_TEXTS = [
        self::PERMISSION_TYPE_MENU => '菜单',
        self::PERMISSION_TYPE_BUTTON => '按钮',
        self::PERMISSION_TYPE_API => '接口',
    ];

    /**
     * 性别
     */
    const GENDER_MALE = 'male';
    const GENDER_FEMALE = 'female';
    const GENDER_OTHER = 'other';

    /**
     * 性别文本
     */
    const GENDER_TEXTS = [
        self::GENDER_MALE => '男',
        self::GENDER_FEMALE => '女',
        self::GENDER_OTHER => '其他',
    ];

    /**
     * 分页默认值
     */
    const PAGINATION_DEFAULT_PER_PAGE = 15;
    const PAGINATION_MAX_PER_PAGE = 100;
    const PAGINATION_ADMIN_DEFAULT = 20;      // 后台默认分页
    const PAGINATION_SHOP_DEFAULT = 12;       // 前台默认分页
    const PAGINATION_LOG_DEFAULT = 50;        // 日志默认分页
    const PAGINATION_NOTIFICATION_DEFAULT = 20; // 通知默认分页

    /**
     * 缓存键前缀
     */
    const CACHE_PREFIX_PRODUCT = 'product:';
    const CACHE_PREFIX_USER = 'user:';
    const CACHE_PREFIX_ORDER = 'order:';
    const CACHE_PREFIX_CART = 'cart:';
    const CACHE_PREFIX_CATEGORY = 'category:';
    const CACHE_PREFIX_SYSTEM = 'system:';

    /**
     * 缓存过期时间（分钟）
     */
    const CACHE_TTL_SHORT = 5;
    const CACHE_TTL_MEDIUM = 60;
    const CACHE_TTL_LONG = 1440;
    const CACHE_TTL_FOREVER = 0;

    /**
     * 文件上传限制
     */
    const UPLOAD_MAX_SIZE_IMAGE = 2048; // KB
    const UPLOAD_MAX_SIZE_DOCUMENT = 10240; // KB
    const UPLOAD_MAX_SIZE_VIDEO = 51200; // KB

    /**
     * 允许的图片格式
     */
    const ALLOWED_IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp'];

    /**
     * 允许的文档格式
     */
    const ALLOWED_DOCUMENT_EXTENSIONS = ['pdf', 'doc', 'docx', 'txt'];

    /**
     * 允许的Excel格式
     */
    const ALLOWED_EXCEL_EXTENSIONS = ['xlsx', 'xls', 'csv'];

    /**
     * 验证规则
     */
    const VALIDATION_PHONE_PATTERN = '/^1[3-9]\d{9}$/';
    const VALIDATION_ID_CARD_PATTERN = '/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/';

    /**
     * 时间格式
     */
    const DATE_FORMAT_DATETIME = 'Y-m-d H:i:s';
    const DATE_FORMAT_DATE = 'Y-m-d';
    const DATE_FORMAT_TIME = 'H:i:s';
    const DATE_FORMAT_MONTH = 'Y-m';
    const DATE_FORMAT_YEAR = 'Y';

    /**
     * 货币符号
     */
    const CURRENCY_SYMBOL = '¥';
    const CURRENCY_CODE = 'CNY';

    /**
     * 默认语言
     */
    const DEFAULT_LANGUAGE = 'zh_CN';

    /**
     * 默认时区
     */
    const DEFAULT_TIMEZONE = 'Asia/Shanghai';

    /**
     * 系统设置键
     */
    const SETTING_SITE_NAME = 'site_name';
    const SETTING_SITE_DESCRIPTION = 'site_description';
    const SETTING_SITE_KEYWORDS = 'site_keywords';
    const SETTING_SITE_LOGO = 'site_logo';
    const SETTING_SITE_FAVICON = 'site_favicon';
    const SETTING_CONTACT_PHONE = 'contact_phone';
    const SETTING_CONTACT_EMAIL = 'contact_email';
    const SETTING_CONTACT_ADDRESS = 'contact_address';
    const SETTING_SOCIAL_WECHAT = 'social_wechat';
    const SETTING_SOCIAL_WEIBO = 'social_weibo';
    const SETTING_SOCIAL_QQ = 'social_qq';
    const SETTING_ANALYTICS_CODE = 'analytics_code';
    const SETTING_MAINTENANCE_MODE = 'maintenance_mode';
    const SETTING_REGISTRATION_ENABLED = 'registration_enabled';
    const SETTING_EMAIL_VERIFICATION = 'email_verification';

    /**
     * 错误代码
     */
    const ERROR_CODE_SUCCESS = 0;
    const ERROR_CODE_VALIDATION = 1001;
    const ERROR_CODE_AUTHENTICATION = 1002;
    const ERROR_CODE_AUTHORIZATION = 1003;
    const ERROR_CODE_NOT_FOUND = 1004;
    const ERROR_CODE_SERVER_ERROR = 1005;
    const ERROR_CODE_BUSINESS_ERROR = 1006;

    /**
     * 错误消息
     */
    const ERROR_MESSAGES = [
        self::ERROR_CODE_SUCCESS => '操作成功',
        self::ERROR_CODE_VALIDATION => '参数验证失败',
        self::ERROR_CODE_AUTHENTICATION => '未登录或登录已过期',
        self::ERROR_CODE_AUTHORIZATION => '权限不足',
        self::ERROR_CODE_NOT_FOUND => '资源不存在',
        self::ERROR_CODE_SERVER_ERROR => '服务器内部错误',
        self::ERROR_CODE_BUSINESS_ERROR => '业务逻辑错误',
    ];

    /**
     * 获取状态文本
     */
    public static function getStatusText(string $type, string $status): string
    {
        $constantName = strtoupper($type) . '_STATUS_TEXTS';
        
        if (!defined('self::' . $constantName)) {
            return $status;
        }
        
        $texts = constant('self::' . $constantName);
        
        return $texts[$status] ?? $status;
    }

    /**
     * 获取所有状态选项
     */
    public static function getStatusOptions(string $type): array
    {
        $constantName = strtoupper($type) . '_STATUS_TEXTS';
        
        if (!defined('self::' . $constantName)) {
            return [];
        }
        
        $texts = constant('self::' . $constantName);
        
        $options = [];
        foreach ($texts as $value => $label) {
            $options[] = ['value' => $value, 'label' => $label];
        }
        
        return $options;
    }

    /**
     * 获取所有类型选项
     */
    public static function getTypeOptions(string $type): array
    {
        $constantName = strtoupper($type) . '_TYPE_TEXTS';
        
        if (!defined('self::' . $constantName)) {
            return [];
        }
        
        $texts = constant('self::' . $constantName);
        
        $options = [];
        foreach ($texts as $value => $label) {
            $options[] = ['value' => $value, 'label' => $label];
        }
        
        return $options;
    }

    /**
     * 获取所有支付方式选项
     */
    public static function getPaymentMethodOptions(): array
    {
        return self::getTypeOptions('PAYMENT_METHOD');
    }

    /**
     * 获取所有售后类型选项
     */
    public static function getAfterSalesTypeOptions(): array
    {
        return self::getTypeOptions('AFTER_SALES_TYPE');
    }

    /**
     * 获取所有订单状态选项
     */
    public static function getOrderStatusOptions(): array
    {
        return self::getStatusOptions('ORDER_STATUS');
    }

    /**
     * 获取所有售后状态选项
     */
    public static function getAfterSalesStatusOptions(): array
    {
        return self::getStatusOptions('AFTER_SALES_STATUS');
    }
}
